﻿using LoveEatRabbit.API.Write.Applications.Commands.Roles;
using LoveEatRabbit.Domain.RBAC;
using LoveEatRabbit.ErrorCode;
using LoveEatRabbit.Infrastructure.Interfaces;
using MediatR;
using Microsoft.OpenApi.Validations.Rules;

namespace LoveEatRabbit.API.Write.Applications.CommandHandlers.Roles
{
    public class DeleteRoleCommandHandler : IRequestHandler<DeleteRoleCommand, APIResult<int>>
    {
        private readonly IBaseRespository<RoleModel> roleRep;

        public DeleteRoleCommandHandler(IBaseRespository<RoleModel> roleRep)
        {
            this.roleRep = roleRep;
        }

        public async Task<APIResult<int>> Handle(DeleteRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            result.Code=APIResultCode.删除成功;
            result.Message=APIResultCode.删除成功.ToString();

            var res=roleRep.GetAll().FirstOrDefault(x=>x.RoleId==request.RoleId);
            res.IsDeleted=true;
            await roleRep.UpdateAsync(res);


            return await Task.FromResult(result);
        }
    }
}
